package cn.liontalk.demo;

/**
 * @author: 周哲
 * @package: cn.liontalk.demo
 * @description: 表示访问同步方法和非同步方法
 * @date: 2019/1/26 14:50
 * @version: V1.0
 */
public class SynchronizedYesAndNo6 implements Runnable {
    static SynchronizedYesAndNo6 instance = new SynchronizedYesAndNo6();

    public void run() {

        if (Thread.currentThread().getName().equals("Thread-0")) {
            methodOne();
        } else {
            methodTwo();
        }
    }


    public synchronized void methodOne() {
        System.out.println("我是加锁的方法 : " + Thread.currentThread().getName());
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + "：运行完成");
    }

    public void methodTwo() {

        System.out.println("我是不加锁的方法 ： " + Thread.currentThread().getName());
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + "：运行完成");

    }

    public static void main(String[] args) {

        Thread thread = new Thread(instance);
        Thread thread1 = new Thread(instance);
        thread.start();
        thread1.start();
        while (thread.isAlive() || thread1.isAlive()) {
        }
        System.out.println("Finished...");


    }
}
